package com.sora.sora.server.service.impl;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sora.sora.common.constant.ResultCode;
import com.sora.sora.common.exception.ApiException;
//import com.sora.sora.common.model.bo.CommentBasicConvert;
//import com.sora.sora.common.model.bo.ReplyBasicConvert;
import com.sora.sora.common.model.bo.CommentBasicConvert;
import com.sora.sora.common.model.bo.ReplyBasicConvert;
import com.sora.sora.common.model.entity.AuthenticationEntity;
import com.sora.sora.common.model.entity.CommentEntity;
import com.sora.sora.common.model.entity.ReplyEntity;
import com.sora.sora.common.model.param.CommentParam;
import com.sora.sora.common.model.param.ReplyParam;
import com.sora.sora.server.mapper.CommentMapper;
import com.sora.sora.common.model.vo.CommentVO;
import com.sora.sora.common.model.vo.ReplyVO;
import com.sora.sora.server.service.CommentService;
import com.sora.sora.security.login.mapper.LoginMapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    LoginMapper loginMapper;

    @Override
    public CommentVO comment(CommentParam commentParam) {
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setContent(commentParam.getContent());
        commentEntity.setUid(commentParam.getUid());
        commentEntity.setTopicId(commentParam.getTopicId());
        commentEntity.setTopicType(commentParam.getTopicType());
        commentEntity.setCommentTime(new Date());

//        if (commentMapper.addComment(commentEntity) > 0) {
//            CommentVO commentVO = CommentBasicConvert.INSTANCE.entityConvertToVO(commentEntity);
//            return commentVO;
//        }
        return null;
    }

    @Override
    public ReplyVO reply(ReplyParam replyParam) {
        ReplyEntity replyEntity = new ReplyEntity();
        replyEntity.setCommentId(replyParam.getCommentId());
        replyEntity.setReplyId(replyParam.getReplyId());
        replyEntity.setReplyType(replyParam.getReplyType());
        replyEntity.setContent(replyParam.getContent());
        replyEntity.setFromId(replyParam.getFromId());
        replyEntity.setToId(replyParam.getToId());
        replyEntity.setReplyTime(new Date());

//        if (commentMapper.addReply(replyEntity) > 0) {
//            return ReplyBasicConvert.INSTANCE.entityConvertToVO(replyEntity);
//        }
        return null;
    }

    @Override
    public boolean deleteComment(int id, int uid) {
        AuthenticationEntity entity = loginMapper.queryById(uid);
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();

        if (userDetails.getUsername().equals(entity.getName())) {
            return commentMapper.delComment(id) > 0;
        }
        throw new ApiException(ResultCode.FORBIDDEN, "不是本人操作");
    }

    @Override
    public boolean deleteReply(int id, int uid) {
        AuthenticationEntity entity = loginMapper.queryById(uid);
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();

        if (userDetails.getUsername().equals(entity.getName())) {
            return commentMapper.delReply(id) > 0;
        }
        throw new ApiException(ResultCode.FORBIDDEN, "不是本人操作");
    }

    @Override
    public List<CommentVO> listAllCommentAscTime(String articleType, int articleId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<CommentEntity> commentEntities = commentMapper.listCommentAscTime(articleType, articleId);
        List<CommentVO> commentVOS = CommentBasicConvert.INSTANCE.entityConvertToVO(commentEntities);
        return commentVOS;
    }

    @Override
    public List<CommentVO> listAllCommentDescTime(String articleType, int articleId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<CommentEntity> commentEntities = commentMapper.listCommentDescTime(articleType, articleId);
        return CommentBasicConvert.INSTANCE.entityConvertToVO(commentEntities);
    }

    @Override
    public List<ReplyVO> listAllReplyAscTime(int commentId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<ReplyEntity> replyEntities = commentMapper.listReplyAscTime(commentId);
        return ReplyBasicConvert.INSTANCE.entityConvertToVO(replyEntities);
    }

    @Override
    public List<ReplyVO> listAllReplyDescTime(int commentId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<ReplyEntity> replyEntities = commentMapper.listReplyDescTime(commentId);
        return ReplyBasicConvert.INSTANCE.entityConvertToVO(replyEntities);
    }

    @Override
    public int countReply(int commentId) {
        return commentMapper.countReply(commentId);
    }

    @Override
    public int countComment(String artcileType, int artcileId) {
        return commentMapper.countComment(artcileType, artcileId);
    }
}
